﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class EmpEduEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id) && id != "undefined")
                {
                    var v = ctx.EmpEducations.Single(r => r.Id == int.Parse(id));
                    StartDate.Value = v.StartDate.ToString("yyyy-MM-dd");
                    if (v.EndDate != null)
                    {
                        EndDate.Value = ((DateTime)v.EndDate).ToString("yyyy-MM-dd");
                    }
                    OrgName.Value = v.OrgName;
                    Major.Value = v.Major;
                    EduLvl.Value = v.EduLvl;
                    CertificateNo.Value = v.CertificateNo;
                    OrgMaster.Value = v.OrgMaster;
                    OrgAddress.Value = v.OrgAddress;
                    EducatedMode.Value = v.EducatedMode;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string id = Request.QueryString["id"];
            EmpEducation empEducation;
            if (!string.IsNullOrEmpty(id) && id != "undefined")
            {
                empEducation = ctx.EmpEducations.Single(r => r.Id == int.Parse(id));
            }
            else
            {
                empEducation = new EmpEducation();
                empEducation.EmpId = int.Parse(Request.QueryString["empId"]);
                ctx.EmpEducations.InsertOnSubmit(empEducation);
            }
            empEducation.StartDate = DateTime.Parse(StartDate.Value);
            if (!string.IsNullOrWhiteSpace(EndDate.Value))
            {
                empEducation.EndDate = DateTime.Parse(EndDate.Value);
            }
            empEducation.OrgName = OrgName.Value;
            empEducation.Major = Major.Value;
            empEducation.EduLvl = EduLvl.Value;
            empEducation.CertificateNo = CertificateNo.Value;
            empEducation.OrgMaster = OrgMaster.Value;
            empEducation.OrgAddress = OrgAddress.Value;
            empEducation.EducatedMode = EducatedMode.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='" + empEducation.Id + "';window.close();</script>");
        }
    }
}